package com.dinggou.shopjb.activitys;


import static com.dinggou.shopjb.utils.RouteUtil.gotoActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.dinggou.shopjb.R;
import com.dinggou.shopjb.adapters.RetailOrderAdapter;
import com.dinggou.shopjb.application.MyApplication;
import com.dinggou.shopjb.base.BaseActivity;
import com.dinggou.shopjb.base.BaseRVAdapter;
import com.dinggou.shopjb.bean.RetailOrderBean;
import com.dinggou.shopjb.net.ActionCallbackListener;
import com.dinggou.shopjb.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class RetailOrderActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener, RetailOrderAdapter.OrderConfirmListener, BaseRVAdapter.OnItemClickListener, View.OnClickListener {
    private RecyclerView recyclerView;
    private TextView no_data_tv,tv_right;
    private SmartRefreshLayout refresh_layout;
    private RadioGroup rg_group;
    private RetailOrderAdapter adapter;
    private List list = new ArrayList<>();
    private int page = 1;
    private String  status = "";
    private String orderType = "RETAIL";

    @Override
    protected void onResume() {
        super.onResume();
        page = 1;
        getListData();
    }

    @Override
    protected int setContentView() {
        return R.layout.retail_order_activity;
    }
    @Override
    protected void findViews() {
        tv_right = $(R.id.tv_right);
        recyclerView = (RecyclerView)$(R.id.retail_order_rv);
        no_data_tv = (TextView)$(R.id.no_data_tv);
        refresh_layout = $(R.id.refresh_layout);
        rg_group = $(R.id.rg_group);
        refresh_layout.setRefreshFooter(new ClassicsFooter(mContext).setSpinnerStyle(SpinnerStyle.Translate));
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new RetailOrderAdapter(mContext,list);
        adapter.setListener(this);
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
    }
    @Override
    protected void setListeners() {
        rg_group.setOnCheckedChangeListener(this);
        tv_right.setOnClickListener(this);
        refresh_layout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getListData();
            }
        });

        refresh_layout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getListData();
            }
        });
    }
    private void getListData(){
        mAppAction = MyApplication.getApplication().getAppAction();
        mAppAction.getRetailOrderList(status, orderType, page, new ActionCallbackListener<List<RetailOrderBean>>() {
            @Override
            public void onSuccess(List<RetailOrderBean> data) {
                refresh_layout.finishRefresh();
                refresh_layout.finishLoadMore();
                if(page == 1){
                    list.clear();
                }
                list.addAll(data);
                if(list.size()>0){
                    no_data_tv.setVisibility(View.GONE);
                    recyclerView.setVisibility(View.VISIBLE);
                }else{
                    no_data_tv.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.GONE);
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                refresh_layout.finishRefresh();
                refresh_layout.finishLoadMore();
                ToastUtil.showToast(mContext,message);
            }
        });
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.rb_all:
                status = "";
                break;
            case R.id.rb_pre_send:
                status = "1";
                break;
            case R.id.rb_pre_get:
                status = "2";
                break;
            case R.id.rb_done:
                status = "3";
                break;
            case R.id.rb_fail:
                status = "9";
                break;
        }
        page = 1;
        getListData();
    }

    @Override
    public void onConfirm(String order_id) {
        mAppAction = MyApplication.getApplication().getAppAction();
        mAppAction.confirmOrder(order_id, new ActionCallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {
                ToastUtil.showToast(mContext,"操作成功");
                page = 1;
                getListData();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showToast(mContext,message);
            }
        });
    }

    @Override
    public void onItemClick(List data, View view, int position, long id) {
        RetailOrderBean bean = (RetailOrderBean)list.get(position);
        Bundle bundle = new Bundle();
        bundle.putSerializable("RetailOrderBean", bean);
        gotoActivity(mContext, RetailOrderInfoActivity.class, bundle);
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.tv_right){
            gotoActivity(mContext,RetailTicketActivity.class,null);
            return;
        }
    }
}
